package org.openslash;

import java.util.concurrent.ArrayBlockingQueue;

import mysgs.SGSMatchConfiguration;
import mysgs.SGSRuleEngine;
import mysgs.cards.SGSCardInstance;

import org.openslash.resource.AResourcePacket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import org.openslash.util.ADBAccessor;
import org.openslash.util.DBAccessorException;
import org.openslash.util.Scanner;
import org.openslash.util.debug;

public class MainActivity extends Activity {
    
	private Thread reThread;
	private SGSRuleEngine engine;
	private TextView tv;
	private Handler msgHandler;
	private AScannerInputMethord as;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Log.i("BestSl", "onCreate");
        initView();
       	initEngine();
       	initIO();

        return;
    }
    
    private void initIO() {
    	Scanner.setUp(as);
       	debug.setUp(new adebug(msgHandler));
	}

	private void initEngine() {
		SGSMatchConfiguration cfg = new SGSMatchConfiguration();
		
		try {
			ADBAccessor.setUp(getResources(), R.raw.sql_event, R.raw.mysgs);
		} catch (DBAccessorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SGSCardInstance cardInstance = 
			new SGSCardInstance(
				new AResourcePacket(getResources().openRawResource(R.raw.cardtext_zh_cn))
		);
		engine = new SGSRuleEngine(cfg, cardInstance);
	}

	@Override
    protected void onStart() {
    	super.onStart();
    	Log.i("BestSl", "onStart");
   		reThread = new Thread(engine,"SGSRuleEngineThread");
   		reThread.start();
    }
    
	@Override
	protected void onRestart() {
		Log.i("BestSl", "onRestart");
		super.onRestart();
	}
	@Override
	protected void onResume() {
		Log.i("BestSl", "onResume");
		super.onResume();
	}
	private void initView() {
		msgHandler = new Handler() {
			@Override
			public void handleMessage(android.os.Message msg) {
				tv.append((CharSequence) msg.obj);
				ScrollView sv = (ScrollView) findViewById(R.id.ScrollView01);
				sv.scrollTo(0, tv.getBottom());
			};
		};
		as = new AScannerInputMethord(new ArrayBlockingQueue<String>(1));
		tv = (TextView) findViewById(R.id.textView);
		Button btnOK = (Button) findViewById(R.id.btnEnter);
		btnOK.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText etMessage = (EditText) findViewById(R.id.etScanner);
				String response = etMessage.getText().toString();
				if( response.length() > 0 )
				{
					if( as.put(response) )
					{
						etMessage.setText("");
						tv.append(response.concat("\n"));
					}
				}
			}
		});
	}

	@Override
	protected void onStop() {
		Log.i("BestSl", "onStop");
		
		if( as.put("interrupt") )
		{
			tv.append("interrupt");
			
			while( !engine.isRunning() )
			{
				Log.i("BestSl", "stop...");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
					break;
				}
			}
			
		}
		Scanner.destroy();
		debug.destroy();
		super.onStop();
	}
	@Override
	protected void onPause() {
		Log.i("BestSl", "onPause");
		super.onPause();
	}
	@Override
	protected void onDestroy() {
		Log.i("BestSl", "onDestroy");
		super.onDestroy();
	}
}